## Project 6: Dance ![](media/b3a76280ed9f1e2243a22822846f0257.jpeg) **Circuit Design** Based on the robot walks circuit, we are able to use the same electrical components and wiring method. Just change the code to make the frog robot dance. **Wiring** Stack the Nano ch340 onto the Nano shield; Connect 4 servo motors to digital pin5,4,3,2 separately. ![](media/image-20230413134328081.png) **Code** ```c #include #include #include #define N_SERVOS 4 //-- First step: Configure the pins where the servos are attached /* --------------- | O O | |---------------| YR 3==> | | <== YL 2 --------------- || || || || RR 5==> ----- ------ <== RL 4 |----- ------| */ #define EEPROM_TRIM false //adjust the initial angles of servos #define TRIM_RR (-5) //right02 #define TRIM_RL 5 //left02 #define TRIM_YR (-5) //right01 #define TRIM_YL 0 //left01 //pins of the servo #define PIN_RR 5 #define PIN_RL 4 #define PIN_YR 3 #define PIN_YL 2 #define INTERVALTIME 10.0 Oscillator servo[N_SERVOS]; #include "SR04.h" #define TRIG_PIN 6 //sending pins of the ultrasonic sensor #define ECHO_PIN 7 //receiving pins of the ultrasonic sensor SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); long a; int i = 0; int val = 0; //each function declaration void goingUp(int tempo); void drunk (int tempo); void noGravity(int tempo); void kickLeft(int tempo); void kickRight(int tempo); void run(int steps, int T=500); void walk(int steps, int T=1000); void backyard(int steps, int T=3000); void backyardSlow(int steps, int T=5000); void turnLeft(int steps, int T=3000); void turnRight(int steps, int T=3000); void moonWalkLeft(int steps, int T=1000); void moonWalkRight(int steps, int T=1000); void crusaito(int steps, int T=1000); void swing(int steps, int T=1000); void upDown(int steps, int T=1000); void flapping(int steps, int T=1000); int t=495; double pause=0; //stop void Stop() { for(int i=0;i<4;i++) servo[i].SetPosition(90); } //go back void backyard(int steps, int T) { int A[4]= {15, 15, 30, 30}; int O[4] = {0, 0, 0, 0}; double phase_diff[4] = {DEG2RAD(0), DEG2RAD(0), DEG2RAD(-90), DEG2RAD(-90)}; for(int i=0;i128)trim=trim-256; Serial.print("TRIM "); Serial.print(x); Serial.print(" en "); Serial.println(trim); servo[x].SetTrim(trim); } } else{ servo[0].SetTrim(TRIM_RR); servo[1].SetTrim(TRIM_RL); servo[2].SetTrim(TRIM_YR); servo[3].SetTrim(TRIM_YL); } for(int i=0;i<4;i++) servo[i].SetPosition(90); } void loop(){ goingUp(2*t); //tiptoe in 2*t for (int i = 0; i <= 2; i = i + (1)) { //set the variable i to save times of rubbing, from 0 to 2, step is 1 segunda1(); // robots rub on the floor } for (int i = 0; i <= 2; i = i + (1)) { //set the variable i to save times of jumping, step is 1 jump(); // robots jump } walk(3,2*t); //go forward for 3 steps in 2*t backyard(3,2*t); //go forward for 3 steps in 2*t crusaito(1,5*t); //slide for 1 step in 5*t crusaito(1,1*t); //slide for 1 step in 1*t crusaito(1,3*t); //slide for 1 step in 3*t for (int i = 0; i <= 2; i = i + (1)) { //set the variable i to save times of swinging, from 0 to 2, step is 1 drunk(4*t); //move in 4*t } moonLEFT(5,2*t); //left moon walking for five steps in 2*t noGravity(2*t); //no gravity in 2*t swing(5,2*t); //swinging for five steps in 2*t moonRight(5,2*t); //right moon walking for five steps in 2*t Stop(); //stop flapping(5,2*t); //flapping for five steps in 2*t Stop(); //stop kickLeft(t); //kick left for (int i = 0; i <= 2; i = i + (1)) { //set the variable i to save times of jumping, step is 1 jump(); //jump } Stop(); //stop kickRight(t); //kick right for (int i = 0; i <= 2; i = i + (1)) { //set the variable i to save times of jumping, step is 1 jump(); //jump } Stop(); //stop } ``` **Note:** should upload the code success first, then plug in Bluetooth module. Otherwise, code upload fails. **Result** Done uploading the code, press down the Power_Switch on Nano Shield. The Otto frog robot will dance. You will see unbelievable dancing movements. ![](media/2df0a13f062faaebf900a76fb9c5162d.jpeg)